package org.jeecg.modules.online.cgform.enhance.impl;

import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.SysCategoryModel;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.enhance.CgformEnhanceJavaListInter;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.service.IOnlCgformFieldService;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Excel导出增强示例： 针对导出的字典，树分类字典、表字典字典，进行文本翻译导出
 */
@Component("cgformEnhanceExportDemo")
public class CgformEnhanceExportDemo implements CgformEnhanceJavaListInter {
    @Autowired
    ISysBaseAPI sysBaseAPI;
    @Autowired
    IOnlCgformFieldService onlCgformFieldService;

    @Override
    public void execute(String tableName, List<Map<String, Object>> data) throws BusinessException {
        //系统全部分类字段
        List<SysCategoryModel> ls = sysBaseAPI.queryAllDSysCategory();
        for (Map<String, Object> map : data) {
            //针对字段fen_tree，进行导出树分类字典显示转换
            String val = oConvertUtils.getString(map.get("fen_tree"));
            if (oConvertUtils.isEmpty(val)) {
                continue;
            }
            List<SysCategoryModel> collect = ls.stream().filter(item -> item.getId().equals(val)).collect(Collectors.toList());
            if (collect != null && collect.size() != 0) {
                map.put("fen_tree", collect.get(0).getName());
            }

            //针对字段sel_search，进行导出表字典显示转换
            String sel_search = oConvertUtils.getString(map.get("sel_search"));
            if (oConvertUtils.isEmpty(sel_search)) {
                continue;
            }
            //获取字典表的配置信息
            OnlCgformField onlCgformField = onlCgformFieldService.queryFormFieldByTableNameAndField(tableName, "sel_search");
            if (onlCgformField == null || oConvertUtils.isEmpty(onlCgformField.getDictTable())) {
                continue;
            }
            List<String> dictTableName = sysBaseAPI.queryTableDictByKeys(onlCgformField.getDictTable(), onlCgformField.getDictText(), onlCgformField.getDictField(), new String[]{sel_search});
            if (dictTableName != null && dictTableName.size() > 0) {
                map.put("sel_search", dictTableName.get(0));
            }
        }

    }
}
